package org.inspiration.dudu.web.controller.user;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.common.pojo.Result;
import org.inspiration.dudu.user.model.dto.UserFavoriteDTO;
import org.inspiration.dudu.web.annotation.Loggable;
import org.inspiration.dudu.web.service.user.UserFavoriteFacadeService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user-favorite")
@Tag(name = "用户收藏内容接口")
@RequiredArgsConstructor
@Loggable

public class UserFavoriteController {

    private final UserFavoriteFacadeService userFavoriteFacadeService;

    @PostMapping
    @Operation(summary = "用户收藏内容")
    public Result<Void> createUserFavorite(@RequestBody UserFavoriteDTO userFavoriteDTO) {
        userFavoriteFacadeService.createUserFavorite(userFavoriteDTO);
        return Result.success();
    }

    @DeleteMapping
    @Operation(summary = "用户取消收藏内容")
    public Result<Void> deleteUserFavorite(@RequestBody UserFavoriteDTO userFavoriteDTO) {
        userFavoriteFacadeService.deleteUserFavorite(userFavoriteDTO);
        return Result.success();
    }

    @GetMapping("/is-favorite")
    @Operation(summary = "判断用户是否收藏该内容")
    public Result<Boolean> isFavorite(@RequestParam("userId") Long userId,
                                      @RequestParam("contentId") String contentId) {
        Boolean isFavorite = userFavoriteFacadeService.isFavorite(userId, contentId);
        return Result.success(isFavorite);
    }

    @GetMapping("/is-folder-favorite")
    @Operation(summary = "判断该收藏夹是否收藏该内容")
    public Result<Boolean> isFolderFavorite(
            @RequestParam("userId") Long userId,
            @RequestParam("contentId") String contentId,
            @RequestParam("folderId") Long folderId
    ) {
        Boolean isFolderFavorite = userFavoriteFacadeService.isFolderFavorite(userId, contentId, folderId);
        return Result.success(isFolderFavorite);
    }

    @GetMapping("/contents")
    @Operation(summary = "获取指定用户收藏夹内的收藏内容(分页)")
    public Result<List<Map<String, Object>>> getFavoriteContents(
            @RequestParam("userId") Long userId,
            @RequestParam("favoriteFolderId") Long favoriteFolderId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        List<Map<String, Object>> list =
                userFavoriteFacadeService.getFavoriteContents
                        (userId, favoriteFolderId, pageNum, pageSize);
        // TODO: 获取收藏内容列表和点赞的内容的列表或者其他什么列表要把文章提问和回答的内容转换为Simple减少运输量
        return Result.success(list);
    }

}
